
#使用shelve模块调整json数据源的结构 


import warnings,os
import shelve 
from chp19_1osconfeed import load
from common import joinRoot

DB_NAME = joinRoot("data",'schedule1_db')
CONFRENCE = 'confrence.115'

class  Record(object):
    def __init__(self,**kwargs):
        self.__dict__.update(**kwargs)

def load_db(db):
    raw_data = load()
    #warnings.warn('loading'+DB_NAME)
    for collection, rec_list in raw_data['Schedule'].items():
        record_type = collection[:-1]
        for record in rec_list:
            key = '{}.{}'.format(record_type,record['serial'])
            record['serial'] = key
            db[key] = Record(**record)



db = shelve.open(DB_NAME)
if CONFRENCE not in db:
    load_db(db)

speaker = db['speaker.3471']
print(dir(speaker)) # dir只列出属性名 vars(speaker) 列出一个完成的属性字典 和speaker.__dict__ 一样